package ass.web;

import java.io.IOException;
import java.io.PrintWriter;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ass.beans.CinemaMovieBean;

import ass.dao.support.CinemaMovieDAOImpl;

/**
 * Servlet implementation class AddMovieTimeServlet
 */
public class AddMovieTimeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public AddMovieTimeServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String dayselect = (String) request.getParameter("dayselect");
		String monthselect = (String) request.getParameter("monthselect");
		String yearselect = (String) request.getParameter("yearselect");
		String hourselect = (String) request.getParameter("hourselect");
		String minuteselect = (String) request.getParameter("minuteselect");

		java.util.Date time = null;

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-hh-mm");

		try {
			time = formatter.parse(yearselect + "-" + monthselect + "-"
					+ dayselect + "-" + hourselect + "-" + minuteselect);

		} catch (ParseException e) {

			e.printStackTrace();
		}

		// int dayselect = Integer.parseInt((String) request
		// .getParameter("dayselect"));
		// int monthselect = Integer.parseInt((String) request
		// .getParameter("monthselect"));
		// int yearselect = Integer.parseInt((String) request
		// .getParameter("yearselect"));
		// int hourselect = Integer.parseInt((String) request
		// .getParameter("hourselect"));
		// int minuteselect = Integer.parseInt((String) request
		// .getParameter("minuteselect"));
		//
		// Date showtime = Date(yearselect, monthselect, dayselect, hourselect,
		// minuteselect);
		System.out.println("dayselect" + dayselect);
		System.out.println("monthselect" + monthselect);
		System.out.println("yearselect" + yearselect);
		System.out.println("hourselect" + hourselect);
		System.out.println("minuteselect" + minuteselect);
		int cinema = Integer.parseInt((String) request.getParameter("cinema"));
		int movie = Integer.parseInt((String) request.getParameter("movie"));

		java.sql.Timestamp showtime = new java.sql.Timestamp(time.getTime());

		CinemaMovieBean cinemamoviebean = new CinemaMovieBean();
		cinemamoviebean.setCinema_id(cinema);
		cinemamoviebean.setMovie_id(movie);
		cinemamoviebean.setShowtime(showtime);

		CinemaMovieDAOImpl cinemamovieDAOImpl = new CinemaMovieDAOImpl();
		cinemamovieDAOImpl.insert(cinemamoviebean);

		response.setContentType("text/html");// from response, set content type
		PrintWriter out = response.getWriter();// from response, get output
												// writer
		out.write("Movie time insert succesful");
		//out.close();
		
		String nextJSP = "";
		nextJSP = "/ownerAddMovie.jsp";
		request.setAttribute("movie_time_inserted",  "<font color='green'>Movie time inserted</font>");
		
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
		dispatcher.forward(request, response);
	}

	// private Date Date(int dayselect, int monthselect, int yearselect,
	// int hourselect, int minuteselect) {
	// // TODO Auto-generated method stub
	// return null;
	// }

}
